﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MegavaniaX.Actors;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace MegavaniaX.UI
{
    /// <summary>
    /// Class for rendering the life bar.
    /// </summary>
    public class Lifebar : UIElement
    {
        private Player _player;

        // 8 pixels wide; height is dynamic
        private const int LbWidth = 8;

        private const int LbXOffset = 16;
        private const int LbYOffset = 8;
        private const int GlobalMaxPlayerEnergy = 20; // player can never have more than this!
        private Texture2D LbBackground;
        private Texture2D LbFill;

        public Lifebar(Player player, Texture2D lbBackground, Texture2D lbFill) : base()
        {
            _player = player;
            LbBackground = lbBackground;
            LbFill = lbFill;

        }

        public override void Draw(SpriteBatch batch, float zoom)
        {
            // height of our lifebar is player's max energy * 2 + 2 (for padding)
            int lbHeight = (_player.MaxEnergy * 2 + 2);
            // our offset changes based on player's max energy vs global max energy
            int additionalYOffset = (GlobalMaxPlayerEnergy - _player.MaxEnergy) * 2;

            int zoomedLbWidth = (int)(LbWidth * zoom);
            // make rectangle
            Rectangle rect = new Rectangle(LbXOffset * (int)zoom, (LbYOffset + additionalYOffset) * (int)zoom, zoomedLbWidth, lbHeight * (int)zoom);
            batch.Draw(LbBackground, rect, Color.White);

            // spriteBatch.Draw(tile.Texture, screenCoordinates, tile.SourceRectangle, Color.White, 0.0f, Vector2.Zero, camera.ZoomFactor, SpriteEffects.None, 0);

            // now draw individual lifebar elements from bottom to top
            int fillX = (LbXOffset + 1) * (int)zoom; // 1px padding
            for (int i = 0; i < _player.Energy; i++)
            {
                int fillY = (int)zoom * (LbYOffset + additionalYOffset + lbHeight - (i * 2) - 2);
                batch.Draw(LbFill, new Vector2(fillX, fillY), null, Color.White, 0.0f, Vector2.Zero, 3.0f, SpriteEffects.None, 0);
            }
        }

    }
}
